﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BiografController;
using BiografController.ServiceReference;
using System.Globalization;

namespace BiografGUI
{
    public partial class SalgGUI : Form
    {
        private KundeController kundeCtr;
        private ForestillingController forestillingCtr;
        private FilmController filmCtr;
        private SalgController salgCtr;
        private SalController salCtr;
        
        private int kundeTlf;
        
        DateTime dato;
        private int sal;
        private string startTid;
        private int filmId;
        private int forestillingId;
        private DateTime fremvisningsDato;
        private int antalBilletter;
        private double totalPris;
        private string salgsType;

        private List<Button> buttons;

        public SalgGUI()
        {
            InitializeComponent();
            kundeCtr = new KundeController();
            filmCtr = new FilmController();
            forestillingCtr = new ForestillingController();
            salgCtr = new SalgController();
            salCtr = new SalController();
            findKundeButton.Enabled = false;
            salgsType = "";
            PopulateFilmComboBox();
            button31.Enabled = false;
            betalKnap.Enabled = false;
            ButtonArray();

        }

        

        private void ButtonArray()
        {
            buttons = new List<Button>(15);
            buttons.Add(button1);
            buttons.Add(button2);
            buttons.Add(button3);
            buttons.Add(button4);
            buttons.Add(button5);
            buttons.Add(button6);
            buttons.Add(button7);
            buttons.Add(button8);
            buttons.Add(button9);
            buttons.Add(button10);
            buttons.Add(button11);
            buttons.Add(button12);
            buttons.Add(button13);
            buttons.Add(button14);
            buttons.Add(button15);
        }

        public void VaelgSaede(Object sender, EventArgs e)
        {
            Button clickedButton = (Button)sender;
            if (clickedButton.BackColor.Equals(Color.LimeGreen))
            {
                clickedButton.BackColor = Color.Blue;
            }
            else if (clickedButton.BackColor.Equals(Color.Blue))
            {
                clickedButton.BackColor = Color.LimeGreen;
            }
        }

        #region Salg
        private void PopulateFilmComboBox()
        {
            List<Film> filmListe = filmCtr.AlleStatusFilm(true);
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = filmListe;
            filmComboBox.DataSource = bindingSource.DataSource;
            filmComboBox.DisplayMember = "Titel";
            filmComboBox.ValueMember = "Titel";
        }
        private void PopulateForestillingsDatoComboBox()
        {
            List<Forestilling> forestillingListe = forestillingCtr.GetAlleForestillingerFraFilmId(filmId);
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = forestillingListe;
            forestillingDatoComboBox.DataSource = bindingSource.DataSource;
            forestillingDatoComboBox.DisplayMember = "FremvisningsDato";
            forestillingDatoComboBox.ValueMember = "FremvisningsDato";
        }
        private void PopulateTider()
        {
            List<Forestilling> forestillingListe = forestillingCtr.GetAlleForestillingerFraDatoOgFilmId(dato, filmId);
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = forestillingListe;
            tidComboBox.DataSource = bindingSource.DataSource;
            tidComboBox.DisplayMember = "start";
            tidComboBox.ValueMember = "start";
        }
        private void PopulateSal()
        {
            List<Forestilling> forestillingListe = forestillingCtr.GetForestillingFraDatoFilmIdOgTid(dato, filmId, startTid);
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = forestillingListe;
            salComboBox.DataSource = bindingSource.DataSource;
            salComboBox.DisplayMember = "sal";
            salComboBox.ValueMember = "sal";
        }
        private void BeregnPris()
        {
            double pris = filmCtr.GetEnFilm(filmId).BilletPris;
            int antalAlmindelig = Convert.ToInt32(numericUpDown1.Value);
            int antalPensionist = Convert.ToInt32(numericUpDown2.Value);
            int antalBoern = Convert.ToInt32(numericUpDown3.Value);
            antalBilletter = antalAlmindelig + antalPensionist + antalBoern;
            totalPris = (pris * antalAlmindelig) + ((pris*0.8)*antalPensionist) + ((pris*0.5)*antalBoern);
            label5.Text = "Total pris: " + Convert.ToString(totalPris);
        }
        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            ValidateTlfTextBox();
        }
        private void ValidateTlfTextBox()
        {
            if (!textBox5.Equals("") && textBox5.Text.Length == 8)
            {
                findKundeButton.Enabled = true;
            }
            else
            {
                findKundeButton.Enabled = false;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Kunde kunde = kundeCtr.GetEnKunde(Convert.ToInt32(textBox5.Text));
            kundeTlf = kunde.TelefonNr;
            string kundeInfo = "Telefon nummer:\t" + kunde.TelefonNr + "\nNavn:\t\t" + kunde.Navn + "\nEmail:\t\t" + kunde.Email;
            billetBox.Text += kundeInfo;
            betalKnap.Enabled = true;

        }
        private void filmComboBox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (filmComboBox.SelectedItem != null)
            {
                // FilmId hentes fra filmComboBox.
                Film film = new Film();
                film = filmCtr.GetEnFilmFraTitel(filmComboBox.SelectedValue.ToString());
                filmId = Convert.ToInt32(film.FilmID);
                PopulateForestillingsDatoComboBox();
                billetBox.Text += "\n\n" + film.Titel;
            }
        }
        private void forestillingDatoComboBox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (forestillingDatoComboBox.SelectedItem != null)
            {
                // ForestillingId hentes fra forestillingComboBox
                string strDate = forestillingDatoComboBox.SelectedValue.ToString();
                dato = Convert.ToDateTime(strDate);
                fremvisningsDato = Convert.ToDateTime(dato);
                PopulateTider();
                billetBox.Text += "\n" + dato.ToShortDateString();
            }
        }
        private void tidComboBox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (tidComboBox.SelectedItem != null)
            {
                startTid = tidComboBox.SelectedValue.ToString();
                forestillingId = forestillingCtr.GetForestillingsIdFraFrilmIdDatoOgTid(filmId, dato, startTid).ForestillingId;
                PopulateSal();
                billetBox.Text += "\n" + startTid;
            }
        }
        private void salComboBox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (salComboBox.SelectedItem != null)
            {
                sal = Convert.ToInt32(salComboBox.SelectedValue.ToString());
                billetBox.Text += "\nSal " + sal.ToString();
                int forestillingsId = forestillingCtr.GetForestillingId(filmId, fremvisningsDato, startTid, sal).ForestillingId;

                CheckLedigeSaeder();
                
            }
        }
        private void opretSalgButton_Click(object sender, EventArgs e)
        {
            int antalSaederValgt = 0;
            salgsType = "Reservation";
            Saede[] s = new Saede[antalSaederValgt];
            if (antalBilletter < 1 || totalPris < 1 || forestillingId < 1)
            {
                MessageBox.Show("Indtast nødvendige felter");
            }
            else
            {
                dato = DateTime.Now;
                salgCtr.NySalg(dato, antalBilletter, salgsType, totalPris, forestillingId, kundeTlf, s);
            }

            int seatcounter = 0;
            
            for (int i = 0; i < buttons.Count; i++)
            {
                if (buttons[i].BackColor.Equals(Color.Blue))
                {
                    buttons[i].BackColor = Color.Yellow;

                    seatcounter = (forestillingId * 15) - 14 + i;
                
                    salCtr.EditSaede(seatcounter, "o");
                    antalSaederValgt++;
                }
            }
        }

        private void Clear()
        {
            dato = DateTime.MinValue;
            numericUpDown1.Value = 1;
            numericUpDown2.Value = 1;
            numericUpDown3.Value = 1;
            totalPris = 0;
            forestillingId = 0;
            kundeTlf = 0;
        }
        private void button1_Click(object sender, EventArgs e)
        {

            BeregnPris();
            billetBox.Text += "\nAntal billetter:\t" + antalBilletter + "\nPris per billet:\t" + filmCtr.GetEnFilm(filmId).BilletPris + "\nTotal pris:\t\t" + totalPris;
        }
        #endregion

        private void button1_Click_1(object sender, EventArgs e)
        {
            VaelgSaede(sender, e);
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            VaelgSaede(sender, e);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            VaelgSaede(sender, e);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            VaelgSaede(sender, e);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            VaelgSaede(sender, e);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            VaelgSaede(sender, e);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            VaelgSaede(sender, e);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            VaelgSaede(sender, e);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            VaelgSaede(sender, e);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            VaelgSaede(sender, e);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            VaelgSaede(sender, e);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            VaelgSaede(sender, e);
        }

        private void button13_Click(object sender, EventArgs e)
        {
            VaelgSaede(sender, e);
        }

        private void button14_Click(object sender, EventArgs e)
        {
            VaelgSaede(sender, e);
        }

        private void button15_Click(object sender, EventArgs e)
        {
            VaelgSaede(sender, e);
        }

        private void betalKnap_Click(object sender, EventArgs e)
        {
            salgsType = "Betalt";
            
            int seatcounter = 0;

            for (int i = 0; i < buttons.Count; i++)
            {
                if (buttons[i].BackColor.Equals(Color.Yellow) || buttons[i].BackColor.Equals(Color.Blue))
                {
                    buttons[i].BackColor = Color.Red;

                    seatcounter = (forestillingId * 15) - 14 + i;

                    salCtr.EditSaede(seatcounter, "s");
                }
            }

        }

        private void CheckLedigeSaeder()
        {
            List<Saede> saeder = new List<Saede>();
            saeder = salCtr.AlleSaederFraForestilling(forestillingId);

            for (int i = 0; i < saeder.Count; i++)
            {
                if (saeder[i].SaedeStatus == "l")
                {
                    buttons[i].BackColor = Color.LimeGreen;
                }
                else if (saeder[i].SaedeStatus == "o")
                {
                    buttons[i].BackColor = Color.Yellow;
                }
                else if (saeder[i].SaedeStatus == "s")
                {
                    buttons[i].BackColor = Color.Red;
                }
            }
        }

    }
}
